<?php


namespace app\admin\service;

use app\admin\model\HtnsFdOrder;

/**
 * Class ReturnPointUserService
 * @package app\admin\service
 * @desc 返点数据相关统计
 */
class ReturnPointUserService
{
    /**
     * @desc 通过用户购买产生的返点金额
     */
    public function buyPayPriceBySourceUid($uid)
    {
        $whereF['source_uid'] = $uid;
        $whereF['delete_time'] = 0;
        $sum = HtnsFdOrder::alias('fo')->field("sum(pay_price) pay_price,sum(grade3_return_price) grade3_return_price,sum(grade4_return_price) grade4_return_price")
            ->where($whereF)->find();
        return $sum;
    }
    /**
     * @desc 获取用户当前获取的返点金额及可用的返点金额
     */
    public function ownReturnPriceGradeuid($uid)
    {
        $sum['ownReturnPrice'] = 0;//获取总返点金额
        $sum['hasReturnPrice'] = 0;//获取当前剩余返点金额
        $whereF2[] = ['grade3_uid|grade4_uid','=',$uid];
        $whereF2[] = ['delete_time','=',0];
        $list = HtnsFdOrder::where($whereF2)->select()->toArray();
        foreach ($list as $v) {
            if($v['grade3_uid'] == $uid){
                if($v['grade3_return_price'] >= 0.01){
                    $sum['ownReturnPrice']+= $v['grade3_return_price'];
                }
                $sum['hasReturnPrice']+= $v['grade3_return_price'];
            }
            if($v['grade4_uid'] == $uid ){
                if($v['grade4_return_price'] >= 0.01){
                    $sum['ownReturnPrice']+= $v['grade4_return_price'];
                }
                $sum['hasReturnPrice']+= $v['grade4_return_price'];
            }
        }
        if($sum['hasReturnPrice']){
            $sum['hasReturnPrice'] = round($sum['hasReturnPrice'],2);
        }
        if($sum['ownReturnPrice']){
            $sum['ownReturnPrice'] = round($sum['ownReturnPrice'],2);
        }
        return $sum;
    }
}